package com.gx.controller;


import com.gx.common.anntation.Log;
import com.gx.common.result.AppResult;
import com.gx.model.dto.UploadFileParamsDto;
import com.gx.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author Liu-pj
 * @Date 2023/3/8 10:46
 * @Desc 大文件上传接口
 **/
@Api(tags = "大文件上传")
@RestController
@RequestMapping("/upload")
public class BigFilesController {


    @Autowired
    FileService fileService;

    @ApiOperation(value = "分块文件上传前的检测")
    @PostMapping("/checkChunk")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileMd5", value = "文件MD5", dataTypeClass = String.class),
            @ApiImplicitParam(name = "totalChunk", value = "分片总数", dataTypeClass = Integer.class)
    })
    @Log(description = "文件操作", type = "分块文件上传前的检测")
    public AppResult<Integer> checkChunk(@RequestParam("fileMd5") String fileMd5,
                                         @RequestParam("totalChunk") Integer totalChunk) {
        return fileService.checkChunk(fileMd5, totalChunk);
    }

    @ApiOperation(value = "上传分块文件")
    @PostMapping(value = "/uploadChunk")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileMd5", value = "文件MD5", dataTypeClass = String.class),
            @ApiImplicitParam(name = "chunk", value = "分片索引", dataTypeClass = Integer.class)
    })
    public AppResult<Boolean> uploadChunk(@RequestPart("file") MultipartFile file,
                                          @RequestParam("fileMd5") String fileMd5,
                                          @RequestParam("chunk") Integer chunk) throws Exception {
        return fileService.uploadChunk(fileMd5, chunk, file.getBytes());

    }

    @ApiOperation(value = "合并文件")
    @PostMapping("/mergeChunks")
    @Log(description = "文件操作", type = "合并文件")
    public AppResult<Boolean> mergeChunks(@RequestBody UploadFileParamsDto dto) {
        return fileService.mergeChunks(dto);
    }

}
